package calcul;

public class LogicOperation {

	public String and(String s1, String s2) throws Exception {
		String result = "";
		if (s1.length() != 32 || s2.length() != 32) {
			throw new Exception("Invalid parameters in LogicOperation class");
		}

		for (int i = 0, len = s1.length(); i < len; i++) {
			if (s1.charAt(i) == '1' && s2.charAt(i) == '1') {
				result += '1';
			} else if ((s1.charAt(i) == '0' && s2.charAt(i) == '1')
					|| (s1.charAt(i) == '1' && s2.charAt(i) == '0')
					|| (s1.charAt(i) == '0' && s2.charAt(i) == '0')) {
				result += '0';
			}
		}
		return result;
	}
	public String not(String s) throws Exception{
		String result = "";
		if (s.length() != 32) {
			throw new Exception("Invalid parameters in LogicOperation class");
		}
		for(int i = 0, len = s.length() ; i < len; i++){
			if(s.charAt(i) == '0'){
				result += '1';
			}else{
				result += '0';
			}
		}
		return result;
	}
	
	public String or(String s1, String s2) throws Exception{
		String result = "";
		if (s1.length() != 32 || s2.length() != 32) {
			throw new Exception("Invalid parameters in LogicOperation class");
		}
		
		for (int i = 0, len = s1.length(); i < len; i++) {
			if (s1.charAt(i) == '0' && s2.charAt(i) == '0') {
				result += '0';
			} else if ((s1.charAt(i) == '0' && s2.charAt(i) == '1')
					|| (s1.charAt(i) == '1' && s2.charAt(i) == '0')
					|| (s1.charAt(i) == '1' && s2.charAt(i) == '1')) {
				result += '1';
			}
		}
		
		return result;
	}
}
